hvm: Clean up console-information passing via xenstore.
authorkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Thu, 1 Mar 2007 13:57:25 +0000 (13:57 +0000)
committerkfraser@localhost.localdomain <kfraser@localhost.localdomain>
Thu, 1 Mar 2007 13:57:25 +0000 (13:57 +0000)
Each serial, parallel and monitor device in qemu that is connected to
a pty creates a xenstore node of the form:
 <domain-path>/monitor/tty
 <domain-path>/serial/<n>/tty
 <domain-path>/parallel/<n>/tty

In addition, serial/0 (com1) also registers its information at:
 <domain-path>/console/tty

Also fix a realloc() failure memory leak.

Signed-off-by: Ben Thomas <ben@virtualiron.com>
tools/ioemu/vl.c

index f355d4d2228192c60e6fce11f43a3b0b0f752364..283e86a4876eb9a8617cc443d88d8fb89cf93db0 100644 (file)
@@ -1565,12 +1565,51 @@ CharDriverState *qemu_chr_open_stdio(void)
     return chr;
 }
 
-int store_console_dev(int domid, char *pts)
+/*
+ * Create a store entry for a device (e.g., monitor, serial/parallel lines).
+ * The entry is <domain-path><storeString>/tty and the value is the name
+ * of the pty associated with the device.
+ */
+static int store_dev_info(char *devName, int domid,
+                          CharDriverState *cState, char *storeString)
 {
     int xc_handle;
     struct xs_handle *xs;
     char *path;
+    char *newpath;
+    FDCharDriver *s;
+    char *pts;
+
+    /* Check for valid arguments (at least, prevent segfaults). */
+    if ((devName == NULL) || (cState == NULL) || (storeString == NULL)) {
+        fprintf(logfile, "%s - invalid arguments\n", __FUNCTION__);
+        return EINVAL;
+    }
 
+    /*
+     * Only continue if we're talking to a pty
+     * Actually, the following code works for any CharDriverState using
+     * FDCharDriver, but we really only care about pty's here
+     */
+    if (strcmp(devName, "pty"))
+        return 0;
+
+    s = cState->opaque;
+    if (s == NULL) {
+        fprintf(logfile, "%s - unable to retrieve fd for '%s'/'%s'\n",
+                __FUNCTION__, storeString, devName);
+        return EBADF;
+    }
+
+    pts = ptsname(s->fd_in);
+    if (pts == NULL) {
+        fprintf(logfile, "%s - unable to determine ptsname '%s'/'%s', "
+                "error %d (%s)\n",
+                __FUNCTION__, storeString, devName, errno, strerror(errno));
+        return errno;
+    }
+
+    /* We now have everything we need to set the xenstore entry. */
     xs = xs_daemon_open();
     if (xs == NULL) {
         fprintf(logfile, "Could not contact XenStore\n");
@@ -1588,14 +1627,19 @@ int store_console_dev(int domid, char *pts)
         fprintf(logfile, "xs_get_domain_path() error\n");
         return -1;
     }
-    path = realloc(path, strlen(path) + strlen("/console/tty") + 1);
-    if (path == NULL) {
+    newpath = realloc(path, (strlen(path) + strlen(storeString) +
+                             strlen("/tty") + 1));
+    if (newpath == NULL) {
+        free(path); /* realloc errors leave old block */
         fprintf(logfile, "realloc error\n");
         return -1;
     }
-    strcat(path, "/console/tty");
+    path = newpath;
+
+    strcat(path, storeString);
+    strcat(path, "/tty");
     if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
-        fprintf(logfile, "xs_write for console fail");
+        fprintf(logfile, "xs_write for '%s' fail", storeString);
         return -1;
     }
 
@@ -1622,7 +1666,6 @@ CharDriverState *qemu_chr_open_pty(void)
     tcsetattr(slave_fd, TCSAFLUSH, &tty);
     
     fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
-    store_console_dev(domid, ptsname(master_fd));
 
     return qemu_chr_open_fd(master_fd, master_fd);
 }
@@ -6694,16 +6737,23 @@ int main(int argc, char **argv)
         fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
         exit(1);
     }
+    store_dev_info(monitor_device, domid, monitor_hd, "/monitor");
     monitor_init(monitor_hd, !nographic);
 
     for(i = 0; i < MAX_SERIAL_PORTS; i++) {
         if (serial_devices[i][0] != '\0') {
+            char buf[16];
             serial_hds[i] = qemu_chr_open(serial_devices[i]);
             if (!serial_hds[i]) {
                 fprintf(stderr, "qemu: could not open serial device '%s'\n", 
                         serial_devices[i]);
                 exit(1);
             }
+            snprintf(buf, sizeof(buf), "/serial/%d", i);
+            store_dev_info(serial_devices[i], domid, serial_hds[i], buf);
+            if (i == 0) /* serial 0 is also called the console */
+                store_dev_info(serial_devices[i], domid,
+                               serial_hds[i], "/console");
             if (!strcmp(serial_devices[i], "vc"))
                 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
         }
@@ -6711,12 +6761,15 @@ int main(int argc, char **argv)
 
     for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
         if (parallel_devices[i][0] != '\0') {
+            char buf[16];
             parallel_hds[i] = qemu_chr_open(parallel_devices[i]);
             if (!parallel_hds[i]) {
                 fprintf(stderr, "qemu: could not open parallel device '%s'\n", 
                         parallel_devices[i]);
                 exit(1);
             }
+            snprintf(buf, sizeof(buf), "/parallel/%d", i);
+            store_dev_info(parallel_devices[i], domid, parallel_hds[i], buf);
             if (!strcmp(parallel_devices[i], "vc"))
                 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
         }